#include <driver/video.h>
#include <klibc/stdio.h>
#include <klibc/string.h>
#include <system/kernel.h>
#include <fs/vfs.h>
#include <shell/var.h>
#include <shell/shell.h>
#include <shell/cmd.h>
#include <system/config.h>

int shell()
{
	char cmd[1024];
	int x;
	
	video_set_attr(THEME_SHELL_COLOR);
	
	//printf("login as: ");
	//scanf("%s",cmd);
	//putchar('\n');

	//strcpy(PROMPT, "~");
	strcpy(PROMPT, "spsh"); //cmd);
	strcat(PROMPT, "$ ");
	x = 0;
	shell_vars = NULL;

	while(1)
	{
		cmd[0] = '\0';
		shell_put_prompt();
		
		scanf("%s",cmd);
		if(shell_parse_command(cmd)) return;
	}
}



/*
 * Analizza ed esegue il commando
 */
int shell_parse_command(char *cmd)
{
	if(strlen(cmd) == 0) return 0;
	int x;
	int sp = 0;
	argc = 0;
	x = 0;
	
	while(*cmd != '\0')
	{
		if(*cmd == ' ')
		{
			if(sp != 1)
			{ 
				sp = 1;
				argv[argc][x] = '\0';
				argc++;
				x = 0;
				cmd++;
				continue;
			}
			else
			{
				cmd++;
				continue;
			}
		}
		sp = 0;
		argv[argc][x] = *cmd;
		cmd++;
		x++;
	}
	argv[argc][x] = '\0';
	argc++;

	
	if(strcmp(argv[0], "help") == 0) cmd_help();
	else if(strcmp(argv[0], "echo") == 0) cmd_echo();
	else if(strcmp(argv[0], "shutdown") == 0) cmd_shutdown();
	else if(strcmp(argv[0], "clear") == 0) cmd_clear();
	else if(strcmp(argv[0], "splogo") == 0) cmd_splogo();
	else if(strcmp(argv[0], "spinfo") == 0) cmd_spinfo();
	else if(strcmp(argv[0], "tower") == 0) cmd_tower();
	else if(strcmp(argv[0], "mod") == 0) mod_command();
	else if(strcmp(argv[0], "tree") == 0) vfs_show_tree(vfs_root, 0);
	else if(strcmp(argv[0], "mount") == 0) vfs_mount_command();
	else if(strcmp(argv[0], "exit") == 0) return 1;
	else printf("command not found\n");
	
	return 0;
}


/*
 * Visualizza il prompt
 */
void shell_put_prompt()
{
	video_set_attr(THEME_PROMPT_COLOR);
	puts(PROMPT);
	video_set_attr(THEME_SHELL_COLOR);
}


/*
 * Aggiunge una funzione
 */
/*void SHELL_ADD_COMMAND(char *name, char *desc, void (*func)())
{
	strcpy(commands[cmdnum].name, name);
	strcpy(commands[cmdnum].desc, desc);
	commands[cmdnum].func = func;
	cmdnum++;
}
*/
